package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
	"wiseverds.com/frame/webport/src/service/agentd_mux1"
	"wiseverds.com/frame/webport/src/service/agentd_ss1"
	"wiseverds.com/frame/webport/src/service/agentd_web"
)

func runStartCmd(cmd *cobra.Command, args []string) {
	fmt.Printf("Starting web server...\n")
	webServer := agentd_web.NewWebServer()
	errChanWeb := webServer.Start()

	fmt.Printf("Starting mux1 server...\n")
	agentd_mux1.DefaultServer = agentd_mux1.NewServer()
	go agentd_mux1.DefaultServer.Start()

	fmt.Printf("Starting ss1 server...\n")
	serverSS1 := agentd_ss1.NewServer(agentd_mux1.DefaultServer.Proxy)
	serverSS1.Start()

	select {
	case err := <-errChanWeb:
		fmt.Fprintf(cmd.ErrOrStderr(), "Error: cannot start web server: %s\n", err)
		os.Exit(1)
	}
}

var startCmd = &cobra.Command{
	Use:   "start",
	Short: fmt.Sprintf("Start the %s service", Program),
	Args:  cobra.MinimumNArgs(0),
	Run:   runStartCmd,
}

func init() {
	rootCmd.AddCommand(startCmd)
}
